package annotation.display;

import java.io.IOException;

import calm.museum.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class videoRecorder extends Activity implements SurfaceHolder.Callback {
	
	private Button cancel1;
	private ImageButton start,stop;
    private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;
	private Camera mCamera ;
    boolean mPreviewRunning = false;
	private MediaRecorder mediaRecorder;
	private final int maxDurationInMs = 20000;
	private final long maxFileSizeInBytes = 500000;
	private String lastFile="",filePath;
	private AlertDialog alert2;
	private TextView descri;
	
	private static final String TAG = "CameraRecord";



	
	public void onCreate(Bundle icicle)  {

		super.onCreate(icicle);
		this.setTitle("Annotation Vid�o");
		setContentView(R.layout.popup_video);
		
		start = (ImageButton) findViewById(R.id.start);
		stop = (ImageButton) findViewById(R.id.stop);
		cancel1 = (Button) findViewById(R.id.cancel);
        mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		
		start.setEnabled(true);
		stop.setClickable(false);
		
		start.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
            	 Toast.makeText(videoRecorder.this, "On enregistre", Toast.LENGTH_SHORT).show();
            	 start.setEnabled(false);
            	 stop.setClickable(true);
            	 
            	 try {
						mCamera.unlock();
			
						mediaRecorder = new MediaRecorder();
			
						mediaRecorder.setCamera(mCamera);
						mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
						mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
			
						mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
			
						mediaRecorder.setMaxDuration(maxDurationInMs);
						filePath = "/ressources/vid" + System.currentTimeMillis() + ".mp4";
						lastFile = Environment.getExternalStorageDirectory()+filePath;
						mediaRecorder.setOutputFile(lastFile);
			
						mediaRecorder.setVideoFrameRate(25);
						mediaRecorder.setVideoSize(640,480);
			
						mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
						mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
			
						mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
			
						mediaRecorder.setMaxFileSize(maxFileSizeInBytes);
			
			                        mediaRecorder.prepare();
						mediaRecorder.start();
			
					} catch (IllegalStateException e) {
						Log.e(TAG,e.getMessage());
						e.printStackTrace();
					} catch (IOException e) {
						Log.e(TAG,e.getMessage());
						e.printStackTrace();
					}
             }
         });
		
		stop.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	try {
        			mediaRecorder.stop();
        		} catch (Exception e) {
        			Log.e(TAG,e.getMessage());
        		}
        		
        		mCamera.lock();
            	
           	 	annot_confirm_video();
           	 	start.setEnabled(true);
           	 	stop.setClickable(false);
           	 	
           	 	
            }
        });
		
		cancel1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
           	 finish();
            }
        });
		

	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		// TODO Auto-generated method stub
		if (mPreviewRunning) {

			mCamera.stopPreview();

			}

			Camera.Parameters p = mCamera.getParameters();

			p.setPreviewSize(640, 480);
			p.setPreviewFormat(PixelFormat.JPEG);
			mCamera.setParameters(p);

			try {

			mCamera.setPreviewDisplay(holder);

			} catch (IOException e) {

			e.printStackTrace();

			}

			mCamera.startPreview();

			mPreviewRunning = true;

		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mCamera = Camera.open();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mCamera.stopPreview();

		mPreviewRunning = false;

		mCamera.release();
		
	}
	
	public void annot_confirm_video(){
		LayoutInflater inflater = LayoutInflater.from(this);
		View layout = inflater.inflate(R.layout.confirm_layout,null);
		AlertDialog.Builder builder;
		
		Button confirm,cancel2;
		
		confirm = (Button) layout.findViewById(R.id.confirm);
		cancel2 = (Button) layout.findViewById(R.id.cancel);
		descri = (TextView)layout.findViewById(R.id.description);
		
		cancel2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
           	 alert2.cancel();
            }
        });
		
		confirm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	/*if (bdd==null) Log.e("ERROR","BDD EMPTY");
        		Annotation annot = new Annotation(descri.getText().toString(),AnnoTX.auteur_format+"User",filePath,String.valueOf(R.drawable.movie));
            	bdd.insertAnnotation(annot);
            	
            	Intent i = new Intent();
                i.setAction(AnnoTX.REFRESH_INTENT);
                sendBroadcast(i);*/
                Log.e("SEND","KRKRKR DO YOU COPY");
            	
                alert2.cancel();
            	finish();
            }
        });
		
		builder = new AlertDialog.Builder(this);
		builder.setView(layout);
		builder.setTitle("Ajouter l'annotation enregistr�e?");
		    	
		alert2 = builder.create();
		alert2.show();
	
	}

}
